package com.mdd.admin.service.image.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.mdd.admin.service.image.IGoodsCommentImageService;
import com.mdd.admin.validate.common.PageParam;
import com.mdd.admin.validate.image.GoodsCommentImageParam;
import com.mdd.admin.vo.image.GoodsCommentImageDetailVo;
import com.mdd.admin.vo.image.GoodsCommentImageListVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.image.GoodsCommentImage;
import com.mdd.common.mapper.image.GoodsCommentImageMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 评论图片实现类
 */
@Service
public class GoodsCommentImageServiceImpl extends ServiceImpl<GoodsCommentImageMapper, GoodsCommentImage> implements IGoodsCommentImageService {

    @Resource
    GoodsCommentImageMapper goodsCommentImageMapper;

    /**
     * 评论图片列表
     *
     * @param pageParam 分页参数
     * @param params    搜索参数
     * @return PageResult<GoodsCommentImageListVo>
     */
    @Override
    public PageResult<GoodsCommentImageListVo> list(PageParam pageParam, Map<String, String> params) {
        Integer page = pageParam.getPageNo();
        Integer limit = pageParam.getPageSize();

        QueryWrapper<GoodsCommentImage> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        goodsCommentImageMapper.setSearch(queryWrapper, params, new String[]{
                "=:commentId@comment_id:int",
                "=:uri:str",
        });

        IPage<GoodsCommentImage> iPage = goodsCommentImageMapper.selectPage(new Page<>(page, limit), queryWrapper);
        List<GoodsCommentImageListVo> list = new LinkedList<>();
        for (GoodsCommentImage item : iPage.getRecords()) {
            GoodsCommentImageListVo vo = new GoodsCommentImageListVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 评论图片详情
     *
     * @param id 主键参数
     * @return GoodsCommentImage
     */
    @Override
    public GoodsCommentImageDetailVo detail(Integer id) {
        GoodsCommentImage model = goodsCommentImageMapper.selectOne(
                new QueryWrapper<GoodsCommentImage>()
                        .eq("id", id)
                        .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        GoodsCommentImageDetailVo vo = new GoodsCommentImageDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 评论图片新增
     *
     * @param goodsCommentImageParam 参数
     */
    @Override
    public void add(GoodsCommentImageParam goodsCommentImageParam) {
        GoodsCommentImage model = new GoodsCommentImage();
        model.setCommentId(goodsCommentImageParam.getCommentId());
        model.setUri(goodsCommentImageParam.getUri());
        goodsCommentImageMapper.insert(model);
    }

    /**
     * 评论图片编辑
     *
     * @param goodsCommentImageParam 参数
     */
    @Override
    public void edit(GoodsCommentImageParam goodsCommentImageParam) {
        GoodsCommentImage model = goodsCommentImageMapper.selectOne(
                new QueryWrapper<GoodsCommentImage>()
                        .eq("id", goodsCommentImageParam.getId())
                        .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(goodsCommentImageParam.getId());
        model.setCommentId(goodsCommentImageParam.getCommentId());
        model.setUri(goodsCommentImageParam.getUri());
        goodsCommentImageMapper.updateById(model);
    }

    /**
     * 评论图片删除
     *
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        GoodsCommentImage model = goodsCommentImageMapper.selectOne(
                new QueryWrapper<GoodsCommentImage>()
                        .eq("id", id)
                        .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        goodsCommentImageMapper.delete(new QueryWrapper<GoodsCommentImage>().eq("id", id));
    }

    @Override
    public List<GoodsCommentImage> listByCommentIds(List<Long> commentIds) {
        if (CollectionUtils.isEmpty(commentIds)) {
            return Lists.newArrayList();
        }
        LambdaQueryWrapper<GoodsCommentImage> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(GoodsCommentImage::getCommentId, commentIds);
        return super.list(lambdaQueryWrapper);
    }

}
